package com.hitqz.robot.biz.controller;

import cn.hutool.core.collection.CollUtil;
import com.hitqz.robot.api.business.enums.FormConfigEnum;
import com.hitqz.robot.api.common.entity.PatrolItemMark;
import com.hitqz.robot.biz.service.FormConfigService;
import com.hitqz.robot.biz.service.PatrolItemMarkService;
import com.hitqz.robot.common.core.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.ExecutorService;

/**
 * @author wengyx
 * @date 2024/8/7
 */

@RestController
@AllArgsConstructor
@RequestMapping("/patrolItemMark")
@Tag(name = "巡检检测项标定数据管理")
public class PatrolItemMarkController {

    private final PatrolItemMarkService patrolItemMarkService;

    private final ExecutorService customThreadPool;

    private final FormConfigService formConfigService;

    @GetMapping("/{id}" )
    @Operation(summary = "根据id查询检测项标定数据")
    public R<PatrolItemMark> getById(@PathVariable("id") Integer id) {
        PatrolItemMark result = patrolItemMarkService.getById(id);
        return R.ok(result);
    }

    @PostMapping
    @Operation(summary = "新增检测项标定数据")
    public R save(@RequestBody @Validated PatrolItemMark dto) {
        patrolItemMarkService.save(dto);
        //同步给服务端
        customThreadPool.submit(()->{
            formConfigService.syncFormMoal(FormConfigEnum.TICKETS_PARAMS_PATROL.getFormName());
        });
        return R.ok();
    }

    @PutMapping
    @Operation(summary = "修改检测项标定数据")
    public R updateById(@RequestBody @Validated PatrolItemMark dto) {
        patrolItemMarkService.updateById(dto);
        //同步给服务端
        customThreadPool.submit(()->{
            formConfigService.syncFormMoal(FormConfigEnum.TICKETS_PARAMS_PATROL.getFormName());
        });
        return R.ok();
    }

    @DeleteMapping
    @Operation(summary = "根据id删除检测项标定数据")
    public R removeById(@RequestBody Integer[] ids) {
        patrolItemMarkService.removeBatchByIds(CollUtil.toList(ids));
        //同步给服务端
        customThreadPool.submit(()->{
            formConfigService.syncFormMoal(FormConfigEnum.TICKETS_PARAMS_PATROL.getFormName());
        });
        return R.ok();
    }

    @GetMapping("/{id}/takePic" )
    @Operation(summary = "检测项标定数据抓图")
    public R<PatrolItemMark> takePic(@PathVariable("id") Integer id){
        return patrolItemMarkService.addPicById(id);
    }

}
